package com.zxy.javaarithmetic.chapter_1_StackQueue;

import java.util.Stack;

/*
 *  @项目名：  study
 *  @包名：    com.zxy.javaarithmetic.chapter_1_StackQueue
 *  @文件名:   P_5_OrderStackByStack
 *  @创建者:   zhangxy
 *  @创建时间:  2018/12/3 11:26
 *  @描述：    将栈由顶到底按从大到小排序，只允许申请一个栈，可以申请变量
 */
public class P_5_OrderStackByStack {

	public static void main(String[] args){
		Stack<Integer> stack = new Stack<>();
		for (int i = 0; i < 10; i++) {
			stack.push((int) (1 + Math.random() * 100));
		}
		System.out.println(stack);

		sortStack(stack);
		System.out.println(stack);
	}

	public static void sortStack(Stack<Integer> stack) {
		Stack<Integer> helpStack = new Stack<>();
		while (!stack.isEmpty()) {
			Integer cur = stack.pop();
			while (!helpStack.isEmpty() && helpStack.peek() < cur) {
				stack.push(helpStack.pop());
			}
			helpStack.push(cur);
		}

		while (!helpStack.isEmpty()) {
			stack.push(helpStack.pop());
		}
	}
}
